home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / spoc88 / dcg / diffrenc.pro < prev    next >
Text File  |  1988-06-14  |  3KB  |  109 lines

  1. /* Parsing by difference lists
  2.    Barbara Clinger, 1988
  3.    
  4.     For input and output, this program is identical to 
  5.     Program Listing 1. However, the expansion of the grammar is
  6.     done with difference lists rather than using append to split
  7.     the list of tokens into noun phrases and verb phrases.
  8. */
  9.  
  10. domains
  11.     toklist = string* 
  12. predicates
  13.     do
  14.     reader(string,toklist)            /* the reader */
  15.     remove_period(toklist,toklist)
  16.     append(toklist,toklist,toklist)
  17. /* The grammar */
  18.     sentence(toklist,toklist)
  19.     noun_phrase(toklist,toklist)
  20.     verb_phrase(toklist,toklist)
  21.     determiner(toklist,toklist)
  22.     noun(toklist,toklist)
  23.     verb(toklist,toklist)
  24.  goal
  25.     do.
  26. clauses
  27.  
  28. do :-
  29.     nl,write("Enter a sentence --> "),
  30.     readln(S),nl,nl,
  31.     reader(S,List),
  32.     write("Output of the reader: ",List),nl,nl,
  33.     remove_period(List,List_in),
  34.     sentence(List_in,List_out),
  35.     write("List out: ",List_out),nl, /* informational write */
  36.     List_out = [].
  37. /* do succeeds when List_out = [], that is, all the list was parsed */
  38.  
  39. /* sentence:
  40.    List_in is a sentence if List_in - Y is a noun phrase and 
  41.    Y - Rest is a verb phrase. If the predicate sentence succeeds
  42.    in parsing the entire list then Rest is the empty list.
  43. */
  44.  
  45. sentence(List_in,Rest) :-
  46.     noun_phrase(List_in,Y),verb_phrase(Y,Rest).    
  47.  
  48. /* noun_phrase
  49.   X is a noun phrase
  50.       if X - Y is a determiner and Y - Rest is a noun,
  51.    or if X - Y is a noun.
  52. */
  53.  
  54. noun_phrase(X,Rest) :- determiner(X,Y),noun(Y,Rest).
  55. noun_phrase(X,Y) :- noun(X,Y).
  56.  
  57. /* verb_phrase
  58.    X is a verb phrase
  59.      if X - Y is a verb and Y - Rest is a noun phrase,
  60.   or if X - Y is a verb and  Y - Rest is a noun
  61.   or if X - Y is a verb.
  62. */
  63.  
  64. verb_phrase(X,Rest) :- verb(X,Y), noun_phrase(Y,Rest).
  65. verb_phrase(X,Rest) :- verb(X,Y), noun(Y,Rest).
  66. verb_phrase(X,Rest) :- verb(X,Rest).
  67.  
  68. /* the dictionary 
  69.  
  70.   Since the predicate determiner is called with determiner(X,Y),
  71.   where X is ["the"|Rest], determiner is saying is that
  72.   "the" is a determiner since ["the"] is ["the"|Rest] - Rest.
  73. */
  74.   
  75. determiner(["the"|Rest],Rest).
  76. determiner([ "a" |Rest],Rest). 
  77.  
  78. noun(["man" |Rest],Rest).
  79. noun(["john"|Rest],Rest).
  80. noun(["mary"|Rest],Rest).
  81. noun(["dog" |Rest],Rest).
  82.  
  83. verb(["likes"|Rest],Rest).
  84. verb(["sees"|Rest],Rest).
  85.  
  86. /* end of dictionary */
  87.  
  88. /* reader
  89.    (1) the empty string returns the empty list,
  90.    (2) if the string is not empty, recursively it takes the front token,
  91.          converts it to lower case, then reads the rest of the list,
  92.          until the string is empty.
  93. */
  94.  
  95. reader("",[]) :- !.        
  96. reader(Str,[Token|Rest]) :-
  97.     fronttoken(Str,Tok,Str1),   
  98.     upper_lower(Tok,Token),
  99.     reader(Str1,Rest),!.
  100.  
  101. /* remove a period at the end of a sentence */
  102. remove_period(L1,L2) :-
  103.     append(L2,["."],L1).
  104. remove_period(L1,L1).        
  105.  
  106. append([],List,List).
  107. append([H|T],L,[H|T2]) :-
  108.     append(T,L,T2).
  109.